<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">
    var map, layer;

    var layerType = 'Aerial';
    var key = "ApTJzdkyN1DdFKkRAE6QIDtzihNaf6IWJsT-nQ_2eMoO4PN__0Tzhl2-WgJtXFSp";

    var options = {
        type: layerType,
        key: key
    };

    function test_constructor(t) {
        t.plan(3);

        var origProcessMetadata = OpenLayers.Layer.Bing.processMetadata;
        var log = [];
        OpenLayers.Layer.Bing.processMetadata = function(metadata) {
            var script = document.getElementById(this._callbackId);
            log.push(script.src);
            origProcessMetadata.apply(this, arguments);
        };
        layer = new OpenLayers.Layer.Bing(OpenLayers.Util.extend({
            metadataParams: {foo: "bar"}
        }, options));
        t.ok(layer instanceof OpenLayers.Layer.Bing, "returns OpenLayers.Layer.Bing object" );
        t.delay_call(5, function() {
            t.eq(log.length, 1, "processMetadata called");
            t.eq(OpenLayers.Util.getParameters(log[0]).foo, "bar", "metadataParams passed to url correctly.");
            OpenLayers.Layer.Bing.processMetadata = origProcessMetadata;
            layer.destroy();
        });
    }

    function test_initLayer(t) {
        t.plan(2);

        var meta = [];
        var origProcessMetadata = OpenLayers.Layer.Bing.processMetadata;
        OpenLayers.Layer.Bing.processMetadata = function(metadata) {
            meta.push(metadata);
        };
        map = new OpenLayers.Map("map");
        layer = new OpenLayers.Layer.Bing(options);
        var extent;
        map.addLayers([layer, new OpenLayers.Layer(null, {
            moveTo: function(bounds, changed) {
                extent = bounds;
            }
        })]);
        map.zoomToMaxExtent();

        var map2 = new OpenLayers.Map("map");
        var layer2 = new OpenLayers.Layer.Bing(OpenLayers.Util.extend({
            initLayer: function() {
                // pretend we have a zoomMin of 2
                this.metadata.resourceSets[0].resources[0].zoomMin = 2;
                OpenLayers.Layer.Bing.prototype.initLayer.apply(this, arguments);
            }
        }, options));
        var extent2;
        map2.addLayers([layer2, new OpenLayers.Layer(null, {
            moveTo: function(bounds, changed) {
                extent2 = bounds;
            }
        })]);
        map2.zoomToMaxExtent();

        t.delay_call(5, function() {
            origProcessMetadata.call(layer, meta[0]);
            t.eq(extent.toBBOX(), map.getExtent().toBBOX(), "layer extent correct for base layer with zoomMin == 1.");
            map.destroy();
        });

        t.delay_call(6, function() {
            origProcessMetadata.call(layer2, meta[1]);
            t.eq(extent2.toBBOX(), map2.getExtent().toBBOX(), "layer extent correct for base layer with zoomMin == 2.");
            map2.destroy();
            OpenLayers.Layer.Bing.processMetadata = origProcessMetadata;
        });
    }

    function test_initLayer_notempty(t) {
        t.plan(1);

        map = new OpenLayers.Map("map", {
            projection: "EPSG:3857",
            layers: [new OpenLayers.Layer("dummy", {isBaseLayer: true})]
        });
        map.zoomToExtent([-14768652, 4492113, -12263964, 5744457]);
        var layer = new OpenLayers.Layer.Bing(OpenLayers.Util.extend({
            isBaseLayer: false
        }, options));
        map.addLayer(layer);

        t.delay_call(5, function() {
            t.ok(layer.grid[0][0].url, "Tile not empty");
            map.destroy();
        });
    }

    function test_attribution(t) {
        t.plan(3);

        var log = [];
        var map = new OpenLayers.Map("map");
        layer = new OpenLayers.Layer.Bing(options);
        map.addLayer(layer);
        map.zoomToMaxExtent();

        t.delay_call(2, function() {
            t.ok(OpenLayers.Util.indexOf(layer.attribution, 'olBingAttribution aerial') !== -1, "Attribution has the correct css class");
            t.ok(OpenLayers.Util.indexOf(layer.attribution, '<img src="">') == -1, "Attribution contains a logo");
            t.ok(OpenLayers.Util.indexOf(layer.attribution, '</img></div></a><a style=') == -1 , "Attribution contains a copyright");
            map.destroy();
        });
    }

    function test_attribution_notempty(t) {
        t.plan(1);

        var log = [];
        var map = new OpenLayers.Map("map");
        layer = new OpenLayers.Layer.Bing(OpenLayers.Util.applyDefaults({type: 'Road'}, options));
        map.addLayer(layer);
        var format = OpenLayers.String.format;
        OpenLayers.String.format = function(tpl, options) {
            log.push(options.copyrights);
        }
        map.zoomToExtent(new OpenLayers.Bounds(-14768652, 4492113, -12263964, 5744457));
        t.delay_call(2, function() {
            t.ok(log.join("") !== "", "Copyright not empty");
            OpenLayers.String.format = format;
            map.destroy();
        });
    }

    function test_getXYZ(t) {
        t.plan(1);

        var map = new OpenLayers.Map("map", {allOverlays: true});
        var osm = new OpenLayers.Layer.OSM();
        map.addLayer(osm);
        map.zoomToExtent(new OpenLayers.Bounds(11373579,-2445208,13628777,680760));
        layer = new OpenLayers.Layer.Bing(options);
        map.addLayer(layer);

        t.delay_call(2, function() {
            var xyz = layer.getXYZ(layer.getTileBounds(new OpenLayers.Pixel(1,1)));
            t.eq(xyz.z, OpenLayers.Util.indexOf(layer.serverResolutions, map.getResolution()), "zoom level correct");
        });
    }

    function test_clone(t) {
        t.plan(1);

        var clone;

        layer = new OpenLayers.Layer.Bing(options);
        clone = layer.clone();
        t.ok(clone instanceof OpenLayers.Layer.Bing, "clone is a Layer.Bing instance");
    }

    function test_protocol(t)
    {
        t.plan(5);

        var map = new OpenLayers.Map("map");
        layer = new OpenLayers.Layer.Bing(options);
        map.addLayer(layer);
        map.zoomToMaxExtent();

        t.delay_call(5, function() {
            t.ok(OpenLayers.Util.indexOf(layer.attribution, '<img src="//') != -1, "Attribution contains a logo with protocol //");
            t.ok(OpenLayers.Util.indexOf(layer.attribution, '<img src="http://') == -1, "Attribution logo does not have http:// protocol");
            t.ok(layer.grid[1][1].url.indexOf('http:') == -1, "Tile url does not contain http:");

            map.destroy();
        });

        var map2 = new OpenLayers.Map("map");
        layer_https = new OpenLayers.Layer.Bing(OpenLayers.Util.applyDefaults({protocol: 'https:'}, options));
        map2.addLayer(layer_https);
        map2.zoomToMaxExtent();

        t.delay_call(5, function() {
            t.ok(OpenLayers.Util.indexOf(layer_https.attribution, '<img src="https://') != -1, "Attribution logo has https:// protocol");
            t.ok(layer_https.grid[1][1].url.indexOf('https:') == 0, "Tile url contains https:");
            map2.destroy();
        });
    }

  </script>
</head>
<body>
<div id="map" style="width:500px;height:550px"></div>
</body>
</html>
